package cn.mymall.config;


import cn.mymall.util.DateUtil;
import cn.mymall.web.interceptor.HoutaiInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.sql.Timestamp;
import java.util.Date;

@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                if(source == null) return null;
                return DateUtil.parseDate(source, DateUtil.PATTERN_YYYY_MM_DD);
            }
        });
        registry.addConverter(new Converter<String, Timestamp>() {
            @Override
            public Timestamp convert(String source) {
                if(source == null) return null;
                return DateUtil.parseTimestamp(source);
            }
        });
    }

    @Bean
    HoutaiInterceptor getHoutaiInterceptor() {
        return new HoutaiInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getHoutaiInterceptor()).addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login/in","/api/user/login/sendCode","/api/user/login/byUUID", "/api/upload/image");
    }
}
